<template>
    <div class="allbox">
            <!-- 功能选择区 -->
            <div class="selection">
            <div @click="refresh(1)">
                <el-button type="info"><el-icon><RefreshRight /></el-icon></el-button>
            </div>
            <div @click="add()">
                <el-button type="success"><el-icon size="22" style="margin-right: 2px;"><Plus /></el-icon>添加</el-button>
            </div>
        </div>
        <!-- table -->
        <div class="table_box">
        <el-table  ref="ref_multipleTableRef"  :data="tableData" height="90vh" @row-dblclick="db_Click"> <!--单击 和 双击 && 单选 和全选 -->
            <el-table-column type="selection" width="50" align="left" />
            <el-table-column label="序号" width="80">
                        <template #default="scope">
                        <div :slot-scope="scope.$index">
                            {{ (scope.$index+1) + ((currentIndex-1)*currentSize) }}
                        </div>
                        </template>
                    </el-table-column>
            <el-table-column label="LOGO" width="180" align="center">
                <template #default="scope">
                    <div style="display: flex; justify-content: center;" :slot-scope="scope.$index">
                        <el-image :src="tableData[scope.$index].img" style="height: 100px;" fit="contain"/> 
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="name" label="品牌名称" width="160" align="left"/>
            <el-table-column prop="company" label="公司" width="180" align="left"/>
            <el-table-column prop="desc" label="品牌描述" min-width="200" align="left"/>
            <el-table-column prop="createdAt" label="创建时间" width="180" align="center" />
            <el-table-column fixed="right" label="执行操作" min-width="200" align="left">
                <template #default="scope">
                   <div>
                        <el-button type="danger" style="width: 150px;"  @click.prevent="del(scope.$index)">
                            <el-icon size="20"><DeleteFilled /></el-icon>删除
                        </el-button>
                   </div>
                </template>
                </el-table-column>
        </el-table>
        <el-pagination
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :page-sizes="pageSize"
            :page-size="pageSize[0]"
            v-model:current-page="currentIndex"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
            class="mt-4"
        />
        </div>
          <!-- form表单 -->
          <el-dialog
            v-model="dialogVisible"
            title="编辑表单"
            draggable
            width="35%">
            <el-form :model="form" class="demo-ruleForm" v-loading="loading" size="large">

            <el-form-item required label="LOGO" label-width="120px">
                <el-input v-model="form.img" type="text" autocomplete="off" placeholder="请输入LOGO链接地址" />
            </el-form-item>

            <el-form-item required label="品牌名称" label-width="120px">
                <el-input v-model="form.name" type="text" autocomplete="off"  placeholder="请输入.."  />
            </el-form-item>

            <el-form-item required label="公司" label-width="120px">
                <el-input v-model="form.company" type="text" autocomplete="off" placeholder="请输入.." />
            </el-form-item>

            <el-form-item required label="品牌描述" label-width="120px">
                <el-input v-model="form.desc" type="textarea" :rows="4" autocomplete="off" placeholder="请输入.." />
            </el-form-item>

            </el-form>
        <template #footer>
            <span class="dialog-footer">
            <el-button type="primary" @click="submit()" style="width: 80%;margin-right: 8%;height: 40px;">
                {{ btn_text }}
            </el-button>
            </span>
        </template>
        </el-dialog>

        <messageComp ref="ref_messageComp"> </messageComp>
    </div>
  
</template>

<script setup>
import axios from "axios"
import { ref } from "vue"
import { useStore } from "vuex";
import messageComp from "../components/messageComp.vue";
const ref_messageComp = ref(null) //弹窗message组件refs
const store = useStore()
// 数据
let loading = ref(false) //加载
let dialogVisible = ref(false) //表单开关
let form = ref({}) //表单数据 el-form
let tableData = ref([]) //表格数据-el-table
let btn_text = ref('添加')
let china_city = ref(["北京", "上海", "天津", "重庆", "河北", "山西", "内蒙古", "辽宁", "吉林", "黑龙江", "江苏",
"浙江", "安徽", "福建", "江西", "山东", "河南", "湖北", "湖南", "广东", "广西", "海南", "四川",
"贵州", "云南", "西藏", "陕西", "甘肃", "宁夏", "青海", "新疆", "香港", "澳门",
"台湾", "Foreign"],)
let orginList = ref([]) //原始数据
let pageSize = ref([7, 10, 50, 99999])
let beforeIndex = ref(1)//之前的索引
let currentIndex = ref(1) //当前的索引
let currentSize = ref(pageSize.value[0]) //当前可视数量
let brandList = ref([]) //品牌列表
let total = ref(0) //总数

const handleSizeChange = (e)=>{
    console.log(e);
    currentSize.value = e
    tableData.value = []
    ref_messageComp.value.loading({dom:'.table_box',text:'Loading..',},500,()=>{
        return handleCurrentChange(currentIndex.value,e);
    })
}
const handleCurrentChange = (e,_pageSize)=>{
    _pageSize = currentSize.value
    console.log(e,_pageSize);
    tableData.value = orginList.value.slice((_pageSize*e) -_pageSize,_pageSize*e)
    beforeIndex.value = e

}

// 提交
const submit = ()=>{
    if (btn_text.value == '添加') {
        addAPI()
    } else {
        updateAPI()
    }
   
}

const db_Click = (e)=>{
    dialogVisible.value = true
    btn_text.value = '修改'
    form.value = e
}

const updateAPI =  () =>{
    dialogVisible.value = false
    // console.log(form.value);
    axios({
        method:'post',
        url:store.state.url + '/brands/update',
        data:form.value,
    }).then(res=>{
        if (res.data.code == 200) {
            ref_messageComp.value.messageSuccess('您更新了一条记录')
        }else{
            ref_messageComp.value.messageFail('更新失败')
        }
    }).catch(err=>{
        ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
    })
}
const addAPI = ()=>{
    dialogVisible.value = false
    axios({
        method:'post',
        url:store.state.url + '/brands/insert',
        data:form.value,
    }).then(res=>{
        if (res.data.code == 200) {
            ref_messageComp.value.messageSuccess('您新增了一条记录')
            refresh()
        }else{
            refresh()
            ref_messageComp.value.messageFail('新增失败')
        }
    }).catch(err=>{
        ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
    })
}
// 获取用户列表
const getData = ()=>{
    axios({
        url:store.state.url + '/brands'
    }).then(res=>{
        console.log(res);
        if (res.data.code == 200) {
            let arr = res.data.data
            total.value = arr.length
            orginList.value = arr
            tableData.value = arr.slice(0,currentSize.value)
        }
    }).catch(err=>{
        ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
    })
}

// 删除
const del = (index)=>{
    loading.value = true
    let timer = setInterval(() => {
            axios({
            method:'post',
            url:store.state.url + '/brands/delete',
            data:{
                id:tableData.value[index].id
            },
        }).then(res=>{
            if (res.data.code == 200) {
                ref_messageComp.value.messageSuccess('您删除了一条记录')
                refresh()
            }else{
                ref_messageComp.value.messageFail('删除失败！')
            }
        }).catch(err=>{
            ref_messageComp.value.messageNetFail('网络连接失败，请检查服务器')
        })
        loading.value = false
        clearInterval(timer)
    }, 1000);

}
// 刷新
const refresh = (n)=>{
    tableData.value = []
    if (n) {
        ref_messageComp.value.loading({dom:'.allbox',text:'数据拉取中..',},1000,()=>{
            ref_messageComp.value.messageSuccess('刷新成功')
            getData()
        })
        return;
    }
    getData()
}
const add = ()=>{
    form.value = {}
    dialogVisible.value = true
    btn_text.value = '添加'

}
// 执行区
refresh()
</script>


<style scoped>
.role{
    position: absolute;
    top: 0%;
    left: 0px;
    width: 100%;
    height: 100%;
    border: 2px solid #ccc;
    color: black;
    border-radius: 10px;
    /* border: 1px solid ; */
}
.selection{
    margin: 10px;
    width: 100%;
    height: 40px;
    line-height: 40px;
    /* border: 2px solid; */
    display: flex;

}
.selection>div{
    width: 70px;
    height: 100%;
    margin-left: 10px;
    border-radius: 5px;
}
/* 刷新 */
.selection>div:nth-child(1){
    margin-left: 40px;
}
.roles{
    width: 40%;
    margin: 5px;
    /* border: 2px solid ; */
}


</style>